/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.graphic.objects;

import controller.commands.SetSelectedCommand;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLayeredPane;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;
import view.GUI.sidepanels.CustomArgument;

/**
 *
 * @author Ben
 */
class CommentTextArea extends JTextArea implements FocusListener, MouseListener,
                                                                    ActionListener{
        private JPanel p;
        private boolean popupMenuClicked;
        private GCommentNode commentNode;
        private JPopupMenu popup;

        public CommentTextArea(GCommentNode commentNode, JPanel p) {
            super(commentNode.getValue(),10,10);
            this.p = p;
            this.commentNode = commentNode;
            this.addFocusListener(this);
            this.addMouseListener(this);
            popup = new JPopupMenu();
            popupMenuClicked = false;

            //construct popup-menu
            JMenuItem m = new JMenuItem();
            m.addActionListener(this);
            m.setText(LabelReader.getInstance().getString("Copy"));
            m.setActionCommand("Copy");
            popup.add(m);
            m = new JMenuItem();
            m.addActionListener(this);
            m.setText(LabelReader.getInstance().getString("Paste"));
            m.setActionCommand("Paste");
            popup.add(m);
            m = new JMenuItem();
            m.addActionListener(this);
            m.setText(LabelReader.getInstance().getString("Cut"));
            m.setActionCommand("Cut");
            popup.add(m);
        }

        public void focusLost(FocusEvent e) {
            if(!popupMenuClicked){
                commentNode.setValue(this.getText());
                GraphicView gv = (GraphicView)commentNode.getParent();
                JLayeredPane jp = commentNode.getRootPane().getLayeredPane();
                RectangleGraphical n = (RectangleGraphical)commentNode;
                gv.notifyObservers(new SetSelectedCommand(n, true));
                gv.notifyObservers(new CustomArgument("tabRight", 0));
                jp.remove(p);
            }
        }

        public void mouseReleased(MouseEvent e) {
            if(e.isPopupTrigger()){
                popupMenuClicked = true;
                popup.show(this, e.getX(), e.getY());
            }
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand().equals("Copy")){
                this.copy();
                popupMenuClicked = false;
            }else if(e.getActionCommand().equals("Paste")){
                this.paste();
                popupMenuClicked = false;
            }else if(e.getActionCommand().equals("Cut")){
                this.cut();
                popupMenuClicked = false;
            }
        }

        public void mouseClicked(MouseEvent arg0) { 
            popupMenuClicked =false;
        }
        public void focusGained(FocusEvent e) { }
        public void mouseEntered(MouseEvent arg0) { }
        public void mouseExited(MouseEvent arg0) { }
        public void mousePressed(MouseEvent arg0) {}
  }